package Tools
{
  import flash.utils.describeType;
  import flash.utils.getDefinitionByName;
  import flash.utils.getQualifiedClassName;
  import mx.managers.BrowserManager;
  import mx.managers.IBrowserManager;
  
  public class UtilFunctions
  {
    public static function setBrowserTitle(str:String):void
    {
      var browser:IBrowserManager = BrowserManager.getInstance();
      if (browser)
      {
        browser.init();
        browser.setTitle(str);
      }
    }
    
    public static function clone(source:Object):Object
    {
      var clone:Object;
      if (source)
      {
        clone = newSibling(source);
        if (clone)
        {
          copyData(source, clone);
        }
      }
      
      return clone;
    }
    
    private static function copyData(source:Object, destination:Object):void
    {
      //copies data from commonly named properties and getter/setter pairs
      if ((source) && (destination))
      {
        try
        {
          var sourceInfo:XML = describeType(source);
          for (var prop:String in source)
          {
            destination[prop] = source[prop];
          }
          
            //for each (prop in sourceInfo.accessor)
            //{
            //  if (prop.@access == "readwrite")
            //  {
            //    if (destination.hasOwnProperty(prop.@name))
            //    {
            //      destination[prop.@name] = source[prop.@name];
            //    } 
            //  }
            //}
        }
        catch (err:Object)
        {
          ;
        }
      }
    }
    
    private static function newSibling(sourceObj:Object):*
    {
      if (sourceObj)
      {
        var objSibling:*;
        try
        {
          var classOfSourceObj:Class = getDefinitionByName(getQualifiedClassName(sourceObj)) as Class;
          objSibling = new classOfSourceObj();
        }
        catch (e:Object)
        {
        }
        
        return objSibling;
      }
      return null;
    }
  
  }
}